import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import { createStore, applyMiddleware  } from 'redux'
import thunk from 'redux-thunk'

// reducer
// 1. reducer 就是一个纯函数
// 2. 接收旧的 state 和 action,返回新的 state
function reFn(state = 10, action) {
  switch (action.type) {
    case 'add':
      let state1 = state
      state1 = state1 + 1
      return state1
    case 'sub':
      let state2 = state
      state2 = state2 - 1
      return state2
    default:
      return state
  }
}
// store->联系axtion和reducer
// 1. createStore(reducer) -> 接收reducer 返回 store
// 2. 获取 state
// 3. 修改 dispatch(action)
const store = createStore(reFn, applyMiddleware(thunk))
store.subscribe(() => {
  ReactDOM.render(<App store={store} />, document.getElementById('root'));
})

// 数据已经交给store->吧store交给App->子组件都可以使用
ReactDOM.render(<App store={store} />, document.getElementById('root'));
